//
//  ZCAddTagViewController.m
//  百思不得姐
//
//  Created by Ning Xie on 16/9/4.
//  Copyright © 2016年 zhangchong. All rights reserved.
//

#import "ZCAddTagViewController.h"
#import "ZCTagButton.h"


#define defaultMargin 5

@interface ZCAddTagViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) UIView *contentView;
@property (weak, nonatomic) UITextField *textField;
@property (weak, nonatomic) UIButton *addTagBtn;

@property (copy, nonatomic) NSMutableString *tagStr;
@property (strong, nonatomic) NSMutableArray *tagStrArray;
@property (strong, nonatomic) NSMutableArray *tagArray;
@end

@implementation ZCAddTagViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNavItem];
    [self setupContentView];
    [self setupTextField];
    
//    [self updateTagBtn];
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.textField becomeFirstResponder];
}

-(void)setupNavItem{
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"添加标签";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonAciton)];
}

-(void)setupContentView{
    
    UIView *contentView = [[UIView alloc]init];
    [self.view addSubview:contentView];
    self.contentView = contentView;
//    contentView.backgroundColor = ZCGlobalRBG;
    
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_topLayoutGuide).offset(defaultMargin);
        make.leading.equalTo(self.view).offset(5);
        make.bottom.trailing.equalTo(self.view).offset(-defaultMargin);
    }];
}
-(void)setupTextField{
    
    UITextField *textField = [[UITextField alloc]init];
    textField.placeholder = @"多个标签用逗号或换行隔开";
    textField.font = [UIFont systemFontOfSize:15];
    textField.textAlignment = NSTextAlignmentLeft;
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    [self.contentView addSubview:textField];
    self.textField = textField;
    textField.delegate = self;
    [textField sizeToFit];
    [textField addTarget:self action:@selector(textFieldEditingChanged) forControlEvents:UIControlEventEditingChanged];
    
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView);
        make.leading.equalTo(self.contentView);
    }];
}
-(UIButton *)addTagBtn{
    
    if (!_addTagBtn) {
        
        UIButton *addTagBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        addTagBtn.backgroundColor = [UIColor blueColor];
        [addTagBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        addTagBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        addTagBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [addTagBtn addTarget:self action:@selector(addTagBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.contentView addSubview:addTagBtn];
        [addTagBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.trailing.leading.equalTo(self.contentView);
            make.top.equalTo(self.textField.mas_bottom).offset(2*defaultMargin);
            
        }];
        _addTagBtn = addTagBtn;
    }
    return _addTagBtn;
}

#pragma mark 监听textField文本变化方法
-(void)textFieldEditingChanged{
    
    if (self.textField.hasText) {
        self.addTagBtn.hidden = NO;
        
        [self.addTagBtn setTitle:[NSString stringWithFormat:@"添加书签:%@",self.textField.text] forState:UIControlStateNormal]; 
        
    }else{
        self.addTagBtn.hidden = YES;
    }
    [self updateTagBtn];
    
}

#pragma mark 添加书签按钮点击事件
-(void)addTagBtnAction:(UIButton*)sender{
    
    ZCTagButton *tagBtn = [[ZCTagButton alloc]init];
    tagBtn.height = self.textField.height;
    [tagBtn setTitle:self.textField.text forState:UIControlStateNormal];
    [self.contentView addSubview:tagBtn];
    [self.tagArray addObject:tagBtn];
    [tagBtn addTarget:self action:@selector(tagBtnAciton:) forControlEvents:UIControlEventTouchUpInside];
    
    sender.hidden = YES;
    [self.tagStrArray addObject:self.tagStr];
    self.tagStr = nil;
    self.textField.text = nil;
    
    [self updateTagBtn];
    
}

-(void)tagBtnAciton:(ZCTagButton*)sender{
    
    [sender removeFromSuperview];
    [self.tagArray removeObject:sender];
    
    
    [UIView animateWithDuration:0.3 animations:^{
        [self updateTagBtn];
    }];
    
    
    
}
-(void)updateTagBtn{
    
    if (self.tagArray.count == 0) {
        [self.textField mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView);
            make.leading.equalTo(self.contentView);
        }];
    }
    
    
    for (NSInteger i = 0; i < self.tagArray.count; i++) {
        
        ZCTagButton *tagBtn = self.tagArray[i];
    
        if (i == 0) {
            tagBtn.x = 0;
            tagBtn.y = 0;
        }else{
            
            ZCTagButton *formerBtn = self.tagArray[i-1];
            CGFloat rightSpace = ScreenWith - 2*defaultMargin - CGRectGetMaxX(formerBtn.frame);
            
            if (rightSpace >= tagBtn.width) {
                
                tagBtn.x = CGRectGetMaxX(formerBtn.frame) + defaultMargin;
                tagBtn.y = formerBtn.y;
            }else{
                tagBtn.x = 0;
                tagBtn.y = CGRectGetMaxY(formerBtn.frame) + defaultMargin;
            }
            
        }
        

        ZCTagButton *lastTagBtn = self.tagArray[self.tagArray.count - 1];

        CGFloat rightSpace = ScreenWith - i*defaultMargin - CGRectGetMaxX(lastTagBtn.frame);
        
        
        if (rightSpace >= [self textFieldWidth] && rightSpace >= self.textField.width ) {
            
            [self.textField mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(tagBtn);
                make.leading.equalTo(tagBtn.mas_trailing).offset(defaultMargin);
            }];
            
        }else{
            [self.textField mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(tagBtn.mas_bottom).offset(defaultMargin);
                make.leading.equalTo(self.contentView);
            }];
        }
        
        
        [self.view layoutIfNeeded];
        
    }
    
}

#pragma mark 确定按钮点击方法
-(void)rightBarButtonAciton{
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark 懒加载控件
-(NSMutableString *)tagStr{
    if (!_tagStr) {
        _tagStr  = [NSMutableString string];
    }
    return _tagStr;
}
-(NSMutableArray *)tagStrArray{
    if (!_tagStrArray) {
        _tagStrArray = [NSMutableArray array];
    }
    return _tagStrArray;
}
-(NSMutableArray *)tagArray{
    if (!_tagArray) {
        _tagArray = [NSMutableArray array];
    }
    return _tagArray;
}

-(CGFloat)textFieldWidth{
    
    CGFloat width = [self.textField.text sizeWithAttributes:@{NSFontAttributeName:self.textField.font}].width;
    ZCLog(@"%f",width);
    return MAX(100, width);
}

#pragma mark UITextField代理方法


@end
